<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: SubscriberListener.cfc 1175 2007-06-25 00:59:44Z pfarrell $
--->
<cfcomponent displayname="SubscriberListener" output="false" extends="MachII.framework.Listener" hint="SubscriberListener for MachBlog">
	<cffunction name="configure" access="public" output="false" returntype="void">
		<!--- don't need anything here for now --->
	</cffunction>
	
	<cffunction name="setSubscriberService" access="public" output="false" returntype="void">
		<cfargument name="subscriberService" type="machblog.org.machblog.subscriber.SubscriberService" required="true" />
		<cfset variables.subscriberService = arguments.subscriberService />
	</cffunction>
	<cffunction name="getSubscriberService" access="public" output="false" returntype="machblog.org.machblog.subscriber.SubscriberService">
		<cfreturn variables.subscriberService />
	</cffunction>
	
	<cffunction name="setNotificationService" access="public" output="false" returntype="void">
		<cfargument name="notificationService" type="machblog.org.machblog.notification.NotificationService" required="true" />
		<cfset variables.notificationService = arguments.notificationService />
	</cffunction>
	<cffunction name="getNotificationService" access="public" output="false" returntype="machblog.org.machblog.notification.NotificationService">
		<cfreturn variables.notificationService />
	</cffunction>
	
	<cffunction name="setUDFs" access="public" output="false" returntype="void" hint="Dependency: injected">
		<cfargument name="udfs" type="machblog.org.machblog.utils.udfs" required="true" />
		<cfset variables.udfs = arguments.udfs />
	</cffunction>
	<cffunction name="getUDFs" access="public" output="false" returntype="machblog.org.machblog.utils.udfs">
		<cfreturn variables.udfs />
	</cffunction>
	
	<cffunction name="getSubscribers" access="public" output="false" returntype="query" 
			hint="Returns a query object containing all subscribers">
		<cfreturn getSubscriberService().getSubscribers() />
	</cffunction>
	
	<cffunction name="getActiveSubscribers" access="public" output="false" returntype="query" 
			hint="Returns a query object containing active subscribers">
		<cfreturn getSubscriberService().getSubscribers(activeOnly = true) />
	</cffunction>
	
	<cffunction name="searchSubscribers" access="public" output="false" returntype="query" 
			hint="Returns a query object containing subscribers matching the search term passed in">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		<cfreturn getSubscriberService().getSubscribers(arguments.event.getArg("searchText")) />
	</cffunction>
	
	<cffunction name="getSubscriptionCount" access="public" output="false" returntype="numeric" 
			hint="Returns the current number of subscribers to the blog">
		<cfreturn getSubscriberService().getSubscriptionCount() />
	</cffunction>
	
	<cffunction name="getSubscriber" access="public" output="false" returntype="machblog.org.machblog.subscriber.Subscriber" 
			hint="Returns a subscriber bean based on the id in the event, or if no id exists, returns an empty bean">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var subscriber = getSubscriberService().getSubscriberBean().init(arguments.event.getArg("subscriberId", "")) />
				
		<cfset getSubscriberService().getSubscriber(subscriber) />
		
		<cfreturn subscriber />
	</cffunction>
	
	<cffunction name="processSubscribeForm" access="public" output="false" returntype="void" 
			hint="Processes the subscribe form and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var email = getNotificationService().getEmailBean() />
		<cfset var moduleName = "" />
		<cfset var am = getAppManager() />
		
		<cfif StructKeyExists(am, "getModuleName")>
			<cfset moduleName = am.getModuleName() />
		</cfif>
		
		<cfswitch expression="#arguments.event.getArg('action')#">
			<cfcase value="subscribe">
				<cftry>
					<cfset arguments.event.getArg("subscriber").getAudit().setDtCreated(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
					<cfset arguments.event.getArg("subscriber").getAudit().setIpCreated(CGI.REMOTE_ADDR) />
					<!--- set to false until they verify --->
					<cfset arguments.event.getArg("subscriber").getAudit().setIsActive(false) />
					
					<!--- set verify id --->
					<cfset arguments.event.getArg("subscriber").setVerifyId(createUUID()) />
					
					<!--- create the new subscriber record --->
					<cfset getSubscriberService().createSubscriber(arguments.event.getArg("subscriber")) />
					
					<!--- send the verification email --->
					<cfset email.setToEmail(arguments.event.getArg("subscriber").getEmail()) />
					<cfset email.setFromEmail(getProperty("emailWebmaster")) />
					<cfset email.setSubject(getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeverifyemailsubject")) />
					<cfset email.setBody(getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeverifyemail")
											& chr(13) & chr(11)
											& variables.udfs.buildUrl(moduleName, 'verifyBlogSubscription', 'email=' & arguments.event.getArg("subscriber").getEmail() & '|verifyId=' & arguments.event.getArg("subscriber").getVerifyId())) />
					<cfset getNotificationService().sendEmail(email) />
					
					<!--- set message that will appear in form --->
					<cfset arguments.event.setArg("subscribeFormMessage", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeverify")) />
					<cfcatch type="any">
						<cfswitch expression="#CFCATCH.Message#">
							<cfcase value="email exists">
								<cfset arguments.event.setArg("subscribeFormMessage", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeemailexists")) />
							</cfcase>
							
							<cfcase value="invalid email">
								<cfset getSubscriberService().deleteSubscriber(arguments.event.getArg("subscriber")) />
								<cfset arguments.event.setArg("subscribeFormMessage", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeinvalidemail")) />
							</cfcase>
							
							<cfdefaultcase>
								<cfset getSubscriberService().deleteSubscriber(arguments.event.getArg("subscriber")) />
								<cfset arguments.event.setArg("subscribeFormMessage", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribefailed")) />
							</cfdefaultcase>
						</cfswitch>
					</cfcatch>
				</cftry>
			</cfcase>
			
			<cfcase value="unsubscribe">
				<cftry>
					<cfset getSubscriberService().deleteSubscriber(arguments.event.getArg("subscriber")) />
					<cfset arguments.event.setArg("subscribeFormMessage", getProperty("resourceBundleService").getResourceBundle().getResource("blogunsubscribesuccessful")) />
					<cfcatch type="any">
						<cfset arguments.event.setArg("subscribeFormMessage", getProperty("resourceBundleService").getResourceBundle().getResource("blogunsubscribefailed")) />
					</cfcatch>
				</cftry>
			</cfcase>

			<cfdefaultcase>
				<cfthrow type="application" message="Invalid Switch Action" 
						detail="Invalid switch action in SubscriberListener, method processSubscribeForm()" />
			</cfdefaultcase>
		</cfswitch>
		
		<cfset announceEvent("showHome", arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="verifyBlogSubscription" access="public" output="false" returntype="void" 
			hint="Verifies a blog subscription using the email address and verification id of the susbcriber">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var success = getSubscriberService().verifyBlogSubscription(arguments.event.getArg("email"), arguments.event.getArg("verifyId")) />
		
		<cfif success>
			<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscriptionverificationsuccess")) />
		<cfelse>
			<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscriptionverificationfailed")) />
		</cfif>
	</cffunction>
	
	<cffunction name="processSubscriberForm" access="public" output="false" returntype="void" 
			hint="Processes the subscriber form and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var subscriber = arguments.event.getArg("subscriber") />
		<cfset var isActive = false />
		
		<cfif arguments.event.isArgDefined("isActive") AND arguments.event.getArg("isActive") NEQ "">
			<cfset isActive = arguments.event.getArg("isActive") />
		</cfif>
		
		<cfset subscriber.getAudit().setIsActive(isActive) />
		
		<cfif subscriber.getSubscriberId() EQ "">
			<cftry>
				<cfset arguments.event.getArg("subscriber").getAudit().setDtCreated(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
				<cfset arguments.event.getArg("subscriber").getAudit().setIpCreated(CGI.REMOTE_ADDR) />
				
				<cfset getSubscriberService().createSubscriber(subscriber) />
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("subscriberaddsucceeded")) />
				<cfcatch type="any">
					<cfswitch expression="#CFCATCH.Message#">
						<cfcase value="email exists">
							<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeemailexists")) />
						</cfcase>
						
						<cfcase value="invalid email">
							<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeinvalidemail")) />
						</cfcase>
						
						<cfdefaultcase>
							<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("subscriberaddfailed")) />
						</cfdefaultcase>
					</cfswitch>
				</cfcatch>
			</cftry>
		<cfelse>
			<cftry>
				<cfset arguments.event.getArg("subscriber").getAudit().setDtModified(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
				<cfset arguments.event.getArg("subscriber").getAudit().setIpModified(CGI.REMOTE_ADDR) />
				
				<cfset getSubscriberService().updateSubscriber(subscriber) />
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("subscriberupdatesucceeded")) />
				<cfcatch type="any">
					<cfswitch expression="#CFCATCH.Message#">
						<cfcase value="email exists">
							<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeemailexists")) />
						</cfcase>
						
						<cfcase value="invalid email">
							<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("blogsubscribeinvalidemail")) />
						</cfcase>
						
						<cfdefaultcase>
							<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("subscriberupdatefailed")) />
						</cfdefaultcase>
					</cfswitch>
				</cfcatch>
			</cftry>
		</cfif>
		
		<cfset announceEvent("showSubscriberForm", arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="deleteSubscriber" access="public" output="false" returntype="void" 
			hint="Deletes a subscriber and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cftry>
			<cfset getSubscriberService().deleteSubscriber(arguments.event.getArg("subscriber")) />
			<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("deletesubscribersucceeded")) />
			<cfcatch type="any">
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("deletesubscriberfailed")) />
			</cfcatch>
		</cftry>
		
		<cfset announceEvent("showSubscriptionAdmin", arguments.event.getArgs()) />
	</cffunction>
	
	<cffunction name="processEmailSubscribersForm" access="public" output="false" returntype="void" 
			hint="Processes the email all subscribers form and announces the next event">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cftry>
			<cfset getSubscriberService().emailSubscribers(getProperty("emailAuthor"), arguments.event.getArg("subject"), 
															arguments.event.getArg("body")) />
			<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("subscriberemailsent")) />
			<cfcatch type="any">
				<cfset arguments.event.setArg("message", getProperty("resourceBundleService").getResourceBundle().getResource("subscriberemailfailed")) />
			</cfcatch>
		</cftry>
		
		<cfset announceEvent("showEmailSubscribersForm", arguments.event.getArgs()) />
	</cffunction>
</cfcomponent>
